import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

class HomeBanner extends StatelessWidget {
  const HomeBanner({super.key, required this.bannerList});

  // 定义列表接收父组件传值
  final List bannerList;

  @override
  Widget build(BuildContext context) {
    if (bannerList.isNotEmpty) {
      return CarouselSlider(
          items: bannerList
              .map((item) => GestureDetector(
                    // 点击操作
                    onTap: () {
                      if (kDebugMode) {
                        print('点击了, id:${item['imgName']}');
                      }
                    },
                    child: Container(
                      margin: const EdgeInsets.all(5),
                      child: Image.network(
                        item['imgUrl'],
                        fit: BoxFit.cover,
                      ),
                    ),
                  ))
              .toList(),
          // 常用配置
          options: CarouselOptions(
            autoPlay: true,
            autoPlayInterval: const Duration(seconds: 2),
            height: 150,
          ));
    } else {
      return Container();
    }
  }
}
